using System;
using System.Runtime.Serialization;

namespace Html
{

	/// <summary>
	/// Represents an html attribute.  An attribute like a class should allow multiple 
	/// values and should be additive.
	/// </summary>
	public class Attribute
	{
		private string _name;

		/// <summary>
		/// Gets or sets the name of the attribute.
		/// </summary>
		/// <value>The name of the attribute.</value>
		public string Name
		{
			get { return _name; }
			set
            {
            	_name = value;
            }
		}
		private string _value;
		/// <summary>
		/// Gets or sets the value of the attribute.
		/// </summary>
		/// <value>The value of the attribute.</value>
		public string Value
		{
			get 
			{
				if (String.IsNullOrEmpty(_value))
					_value = "";
				return _value; 
			}
			set
            {
            	_value = value;
            }
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="Attribute"/> class.
		/// </summary>
		public Attribute()
		{
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="Attribute"/> class.
		/// </summary>
		/// <param name="name">The name.</param>
		/// <param name="value">The value.</param>
		public Attribute(string name, object value)
		{
			_name = name;
			if (value != null)
				_value = value.ToString();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="Attribute"/> class.
		/// </summary>
		/// <param name="nameValuePairsSeparatedByAnEqualsSign">The name value pair separated by an equals sign.</param>
		public Attribute(string nameValuePairsSeparatedByAnEqualsSign)
		{
			if (String.IsNullOrEmpty(nameValuePairsSeparatedByAnEqualsSign))
				throw new ArgumentException("nameValuePairSeparatedByEqualsSign is null or empty.", "nameValuePairsSeparatedByAnEqualsSign");

			string[] nameValues = nameValuePairsSeparatedByAnEqualsSign.Replace("==", "&deq;").Split('=');
			_name = nameValues[0];
			if (nameValues.Length > 1)
				_value = nameValues[1].Replace("&deq;", "==");
		}

	}
}
